<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="utf-8">
    <title>博客列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layuimini/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" media="all">

    <style>
        .flag-span {
            display:inline-block;
            height:25px;
            line-height:25px;
            padding:0 8px;
            border:1px solid transparent;
            color:#fff;
            text-align:center;
            font-size:12px;
            border-radius:2px;
        }
        .flag-span-green {
            background-color:#5FB878;
        }
        .flag-span-orange {
            background-color:#fe9c5b;
        }
    </style>

</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">标题</label>
                            <div class="layui-input-inline">
                                <input type="text" name="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">类型</label>
                            <div class="layui-input-inline">
                                <select name="flag">
                                    <option value="">请选择类型</option>
                                    <option value="1">原创</option>
                                    <option value="0">转载</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <input type="checkbox" name="published" value="1" title="发布">
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="flag">
            {{# if (d.flag == 1) { }}
            <span class="flag-span flag-span-green"> 原创 </span>
            {{# } else if (d.flag == 0) { }}
            <span class="flag-span flag-span-orange"> 转载 </span>
            {{# } }}
        </script>

        <script type="text/html" id="cover">
            <img src="{{d.cover}}" />
        </script>

        <script type="text/html" id="published">
            <input type="checkbox" name="published" pid={{d.id}} lay-filter="published" lay-skin="switch" value="{{d.published}}" lay-text="发布|草稿" {{d.published == '1' ? 'checked' : ''}}>
        </script>

        <script type="text/html" id="appreciate">
            <input type="checkbox" name="appreciate" aid={{d.id}} lay-filter="appreciate" lay-skin="switch" value="{{d.appreciate}}" lay-text="打赏|免费" {{d.appreciate == '1' ? 'checked' : ''}}>
        </script>

        <script type="text/html" id="commentable">
            <input type="checkbox" name="commentable" cid={{d.id}} lay-filter="commentable" lay-skin="switch" value="{{d.commentable}}" lay-text="开启|关闭" {{d.commentable == '1' ? 'checked' : ''}}>
        </script>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<script th:src="@{/layuimini/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script>

    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        function tableRender() {
            table.render({
                elem: '#currentTableId',
                // url: '/layuimini/api/blog.json',
                url: '/admin/blog/selectBlogList',
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                parseData: function (res) {
                    return {
                        "code": res.code,
                        "msg": res.msg,
                        "count": res.count,
                        "data": res.data.blogs
                    }
                },
                cols: [
                    [
                        {type: "checkbox", width: 50},
                        {field: 'id', width: 70, title: 'ID', align: "center"},
                        {field: 'title', width: 100, title: '标题', align: "center"},
                        {field: 'views', width: 100, title: '浏览量', align: "center"},
                        {field: 'flag', width: 80, title: '类型', align: "center", templet: "#flag"},
                        {field: 'published', width: 100, title: '状态', align: "center", templet: "#published"},
                        {field: 'appreciate', width: 100, title: '赞赏', align: "center", templet: "#appreciate"},
                        {field: 'commentable', width: 100, title: '评论', align: "center", templet: "#commentable"},
                        {field: 'cover', width: 210, title: '封面', templet: "#cover", align: "center"},
                        {field: 'createTime', width: 170, title: '创建时间', align: "center"},
                        {field: 'updateTime', width: 170, title: '修改时间', align: "center"},
                        {title: '操作', minWidth: 140, toolbar: '#currentTableBar', align: "center", fixed: 'right'}
                    ]
                ],
                limits: [5, 10, 25, 50],
                limit: 5,
                page: true,
                skin: 'line'
            });
        }

        tableRender()

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = data.field

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: result
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '博客添加',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/admin/blog/blog-add',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                var checkCount = checkStatus.data.length
                if (checkCount === 0) {
                    layer.msg('批量删除至少选中一项数据');
                    return false;
                }
                layer.confirm('真的要删除选中的项吗？',function() {
                    var idArrays = [];
                    for (var i = 0; i < checkCount; i++) {
                        idArrays.push(checkStatus.data[i].id)
                    }
                    var ids = ""
                    idArrays.forEach(function (value) {
                        ids += value + ","
                    })
                    ids = ids.substring(0, ids.length - 1)
                    $.get('/admin/blog/deleteBlogs/' + ids, function (res) {
                        if (res.code === 200) {
                            //刷新表格
                            tableRender()
                            layer.msg(res.msg)
                        } else if (res.code === 444) {
                            layer.msg(res.msg)
                        }
                    })
                })
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var index = layer.open({
                    title: '博客修改',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/admin/blog/blog-edit/' + data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function () {
                    $.get('/admin/blog/deleteBlog/' + data.id, function (res) {
                        if (res.code === 200) {
                            tableRender()
                            layer.msg(res.msg)
                        } else if (res.code === 444) {
                            layer.msg(res.msg)
                        }
                    })
                });
            }
        });

        //表修改属性
        form.on('switch(published)', function(obj){
            // 通过属性获取绑定的id值
            var id = $(this).attr('pid')
            // 判断开关的状态
            var published = obj.elem.checked?"1":"0"
            // 构造请求参数对象
            var data = new Object()
            data.id = id
            data.published = published

            $.post('/admin/blog/editPublished', data, function (res) {
                if (res.code === 200) {
                    tableRender()
                    layer.msg(res.msg)
                } else if (res.code === 444) {
                    layer.msg(res.msg)
                }
            })
        });

        form.on('switch(appreciate)', function(obj){
            // 通过属性获取绑定的id值
            var id = $(this).attr('aid');
            // 判断开关的状态
            var appreciate = obj.elem.checked?"1":"0";
            // 构造请求参数对象
            var data = new Object()
            data.id = id
            data.appreciate = appreciate

            $.post('/admin/blog/editAppreciate', data, function (res) {
                if (res.code === 200) {
                    tableRender()
                    layer.msg(res.msg)
                } else if (res.code === 444) {
                    layer.msg(res.msg)
                }
            })
        });

        form.on('switch(commentable)', function(obj){
            // 通过属性获取绑定的id值
            var id = $(this).attr('cid');
            // 判断开关的状态
            var commentable = obj.elem.checked?"1":"0";
            // 构造请求参数对象
            var data = new Object()
            data.id = id
            data.commentable = commentable

            $.post('/admin/blog/editCommentable', data, function (res) {
                if (res.code === 200) {
                    tableRender()
                    layer.msg(res.msg)
                } else if (res.code === 444) {
                    layer.msg(res.msg)
                }
            })
        });

    });


</script>

</body>
</html>
